﻿using Microsoft.AspNetCore.Components.Web;
using Microsoft.EntityFrameworkCore;
using UsedBookTradeSystem.Models.Entity;
using UsedBookTradeSystem.Models.Enum;

namespace UsedBookTradeSystem.DBContext
{
    public class SystemDbContext : DbContext
    {
        public DbSet<User> Users { get; set; }
        public DbSet<Book> Books { get; set; }
        public DbSet<Order> Orders { get; set; }
        public DbSet<Cart> Carts { get; set; }

        public DbSet<BookInfo> BooksInfo { get; set; }
        public DbSet<BookCategory> BookCategories { get; set; }

        public SystemDbContext(DbContextOptions<SystemDbContext> options) : base(options) { }
        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            optionsBuilder.UseMySql(
                "server=localhost;port=3306;uid=root;pwd=root;database=UsedBookTradeSys",
                new MySqlServerVersion(new Version(8,0,31)));
            base.OnConfiguring(optionsBuilder);
        }

        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            modelBuilder.UseCollation("utf8_general_ci")
                .HasCharSet("utf8");
            modelBuilder.Entity<User>(entity => 
            {
                entity.HasKey(x => x.Id);
                entity.Property(x => x.PasswordHash)
                .HasColumnName("password");
                entity.Ignore(x => x.PassWord);
                entity.HasMany<Order>(user => user.orderList)
                .WithOne(order => order.User);
                entity.HasQueryFilter(e => e.IsDeleted == false);
                entity.HasData(
                    new User 
                    { 
                        Id=1,
                        UserName="Admin",
                        PassWord="123456",
                        Gender=GenderEnum.Unknown,
                        Role=RoleEnum.Admin,
                        Tel="15320984655"
                    },
                    new User
                    {
                        Id = 2,
                        UserName = "TestUser",
                        PassWord = "123456",
                        Gender = GenderEnum.Unknown,
                        Role = RoleEnum.User,
                        Tel = "12320958995"
                    });
            });
            modelBuilder.Entity<BookInfo>(entity =>
            {
                entity.HasKey(x => x.Id);
                entity.HasQueryFilter(e => e.IsDeleted == false);
                entity.HasMany<Book>(bookInfo => bookInfo.Books)
                .WithOne(book => book.BookInfo);
            });
            modelBuilder.Entity<Book>(entity =>
            {
                entity.HasKey(x => x.Id);
            });
            modelBuilder.Entity<BookCategory>(entity =>
            {
                entity.HasKey(x => x.Id);
                entity.HasMany<BookInfo>(category => category.BookList)
                .WithOne(book => book.Category);
                entity.HasData(
                    new BookCategory { Id=1,CategoryName= "文学艺术" }
                    );
                entity.HasQueryFilter(e => e.IsDeleted == false);
            });
            modelBuilder.Entity<Order>(entity =>
            {
                entity.HasKey(x => x.Id);
                entity.Ignore(x => x.Total);
                entity.HasMany<Book>(order => order.BookList)
                .WithOne(book=>book.Order);
            });
            modelBuilder.Entity<Cart>(entity =>
            {
                entity.HasKey(x => x.Id);
                entity.Ignore(x => x.Total);
                entity.HasMany<Book>(x => x.Books).WithOne(x=>x.Cart);
                entity.HasOne(x => x.User).WithOne(user=>user.Cart)
                .HasForeignKey<Cart>(x=>x.UserId);
            });
            base.OnModelCreating(modelBuilder);
        }
    }
}
